<?php
/**
 * Created by PhpStorm.
 * User: trnall
 * Date: 2019/9/3
 * Time: 15:37
 */

namespace wechat;


use app\api\lib\exception\ErrorException;

class CheckInput
{
    private $people;    //人名
    private $salacity;  //色情
    private $abuse;     //骂人
    private $superstition;//迷信
    private $other;  //其他

    public function __construct()
    {
        $this->people = '办理pos,理p,pos,POS,信用卡,套现,胡的接班人,钦定接班人,习近平,平近习,xjp,习太子,习明泽,老习,温家宝,温加宝,温x,温jia宝,温宝宝,温加饱,温加保,张培莉,温云松,温如春,温jb,胡温,胡x,胡jt,胡boss,胡总,胡王八,hujintao,胡jintao,胡j涛,胡惊涛,胡景涛,胡紧掏,湖紧掏,胡紧套,锦涛,hjt,胡派,胡主席,刘永清,胡海峰,胡海清,江泽民,民泽江,江胡,江哥,江主席,江书记,江浙闽,江沢民,江浙民,择民,则民,茳泽民,zemin,ze民,老江,老j,江core,江x,江派,江zm,jzm,江戏子,江蛤蟆,江某某,江贼,江猪,江氏集团,江绵恒,江绵康,王冶坪,江泽慧,邓小平,平小邓,xiao平,邓xp,邓晓平,邓朴方,邓榕,邓质方,毛泽东,猫泽东,猫则东,chairmanmao,猫贼洞,毛zd,毛zx,z东,ze东,泽d,zedong,毛太祖,毛相,主席画像,改革历程,朱镕基,朱容基,朱镕鸡,朱容鸡,朱云来,李鹏,李peng,里鹏,李月月鸟,李小鹏,李小琳,华主席,华国,国锋,国峰,锋同志,白春礼,薄熙来,薄一波,蔡赴朝,蔡武,曹刚川,常万全,陈炳德,陈德铭,陈建国,陈良宇,陈绍基,陈同海,陈至立,戴秉国,丁一平,董建华,杜德印,杜世成,傅锐,郭伯雄,郭金龙,贺国强,胡春华,耀邦,华建敏,黄华华,黄丽满,黄兴国,回良玉,贾庆林,贾廷安,靖志远,李长春,李春城,李建国,李克强,李岚清,李沛瑶,李荣融,李瑞环,李铁映,李先念,李学举,李源潮,栗智,梁光烈,廖锡龙,林树森,林炎志,林左鸣,令计划,柳斌杰,刘奇葆,刘少奇,刘延东,刘云山,刘志军,龙新民,路甬祥,罗箭,吕祖善,马飚,马恺,孟建柱,欧广源,强卫,沈跃跃,宋平顺,粟戎生,苏树林,孙家正,铁凝,屠光绍,王东明,汪东兴,王鸿举,王沪宁,王乐泉,王洛林,王岐山,王胜俊,王太华,王学军,王兆国,王振华,吴邦国,吴定富,吴官正,无官正,吴胜利,吴仪,奚国华,习仲勋,徐才厚,许其亮,徐绍史,杨洁篪,叶剑英,由喜贵,于幼军,俞正声,袁纯清,曾培炎,曾庆红,曾宪梓,曾荫权,张德江,张定发,张高丽,张立昌,张荣坤,张志国,赵洪祝,周生贤,周永康,朱海仑,政治局常委,中纪委,主席像,总书记,中南海,大陆当局,中国当局,北京当局,共产党,党产共,gcd,共贪党,gongchandang,阿共,共一产一党,产党共,公产党,工产党,共c党,共x党,共铲,供产,共惨,供铲党,供铲谠,供铲裆,共残党,共残主义,共产主义的幽灵,拱铲,老共,中共,中珙,中gong,gc党,贡挡,gong党,g产,狗产蛋,共残裆,恶党,邪党,共产专制,共产王朝,裆中央,土共,土g,共狗,g匪,共匪,仇共,communistparty,政府,症腐,政腐,政付,正府,政俯,政一府,政百度府,政f,zhengfu,政zhi,挡中央,档中央,中央领导,中国zf,中央zf,国wu院,中华帝国,gong和,大陆官方,北京政权';
        $this->salacity = '爱女人,爱液,按摩棒,拔出来,爆草,包二奶,暴干,暴奸,暴乳,爆乳,暴淫,屄,被操,被插,被干,逼奸,仓井空,插暴,操逼,操黑,操烂,肏你,肏死,操死,操我,厕奴,插比,插b,插逼,插进,插你,插我,插阴,潮吹,潮喷,成人电影,成人论坛,成人色情,成人网站,成人文学,成人小说,艳情小说,成人游戏,吃精,赤裸,抽插,扌由插,抽一插,春药,大波,大力抽送,大乳,荡妇,荡女,盗撮,多人轮,发浪,放尿,肥逼,粉穴,封面女郎,风月大陆,干死你,干穴,肛交,肛门,龟头,裹本,国产av,好嫩,豪乳,黑逼,后庭,后穴,虎骑,花花公子,换妻俱乐部,黄片,几吧,鸡吧,鸡巴,鸡奸,寂寞男,寂寞女,妓女,激情,集体淫,奸情,叫床,脚交,金鳞岂是池中物,金麟岂是池中物,精液,就去日,巨屌,菊花洞,菊门,巨奶,巨乳,菊穴,开苞,口爆,口活,口交,口射,口淫,裤袜,狂操,狂插,浪逼,浪妇,浪叫,浪女,狼友,聊性,流淫,铃木麻,凌辱,漏乳,露b,乱交,乱伦,轮暴,轮操,轮奸,裸陪,买春,美逼,美少妇,美乳,美腿,美穴,美幼,秘唇,迷奸,密穴,蜜穴,蜜液,摸奶,摸胸,母奸,奈美,奶子,男奴,内射,嫩逼,嫩女,嫩穴,捏弄,女优,炮友,砲友,喷精,屁眼,品香堂,前凸后翘,强jian,强暴,强奸处女,情趣用品,情色,拳交,全裸,群交,惹火身材,人妻,人兽,日逼,日烂,肉棒,肉逼,肉唇,肉洞,肉缝,肉棍,肉茎,肉具,揉乳,肉穴,肉欲,乳爆,乳房,乳沟,乳交,乳头,三级片,骚逼,骚比,骚女,骚水,骚穴,色逼,色界,色猫,色盟,色情网站,色区,色色,色诱,色欲,色b,少年阿宾,少修正,射爽,射颜,食精,释欲,兽奸,兽交,手淫,兽欲,熟妇,熟母,熟女,爽片,爽死我了,双臀,死逼,丝袜,丝诱,松岛枫,酥痒,汤加丽,套弄,体奸,体位,舔脚,舔阴,调教,偷欢,偷拍,推油,脱内裤,文做,我就色,无码,舞女,无修正,吸精,夏川纯,相奸,小逼,校鸡,小穴,小xue,写真,性感妖娆,性感诱惑,性虎,性饥渴,性技巧,性交,性奴,性虐,性息,性欲,胸推,穴口,学生妹,穴图,亚情,颜射,阳具,杨思敏,要射了,夜勤病栋,一本道,一夜欢,一夜情,一ye情,阴部,淫虫,阴唇,淫荡,阴道,淫电影,阴阜,淫妇,淫河,阴核,阴户,淫贱,淫叫,淫教师,阴茎,阴精,淫浪,淫媚,淫糜,淫魔,淫母,淫女,淫虐,淫妻,淫情,淫色,淫声浪语,淫兽学园,淫书,淫术炼金士,淫水,淫娃,淫威,淫亵,淫样,淫液,淫照,阴b,应召,幼交,幼男,幼女,欲火,欲女,玉女心经,玉蒲团,玉乳,欲仙欲死,玉穴,援交,原味内衣,援助交际,张筱雨,招鸡,招妓,中年美妇,抓胸,自拍,自慰,作爱,18禁,99bb,a4u,a4y,adult,amateur,anal,a片,fuck,gay片,g点,g片,hardcore,h动画,h动漫,incest,porn,secom,sexinsex,sm女王,xiao77,xing伴侣,tokyohot,yin荡';
        $this->abuse = '贱人,装b,大sb,大SB,傻逼,傻b,煞逼,煞笔,刹笔,傻比,沙比,欠干,婊子养的,我日你,我操,我草,卧艹,卧槽,爆你菊,艹你,cao你,你他妈,真他妈,别他吗,草你吗,草你丫,操你妈,擦你妈,操你娘,操他妈,日你妈,干你妈,干你娘,娘西皮,狗操,狗草,狗杂种,狗日的,操你祖宗,操你全家,操你大爷,妈逼,你麻痹,麻痹的,妈了个逼,马勒,狗娘养,贱比,贱b,下贱,死全家,全家死光,全家不得好死,全家死绝,白痴,无耻,sb,杀b,你吗b,你妈的,婊子,贱货,人渣,混蛋,媚外,和弦,限量,铃声,性伴侣,男公关,火辣,精子,射精,诱奸,强奸,做爱,性爱,发生关系,按摩,快感,处男,猛男,少妇,屌,屁股,下体,a片,内裤,浑圆,咪咪,发情,刺激,白嫩,粉嫩,兽性,风骚,呻吟,sm,阉割,高潮,裸露,不穿,一丝不挂,脱光,干你,干死,我干';
        $this->superstition = '福音会,中国教徒,统一教,观音法门,清海无上师,盘古,李洪志,志洪李,李宏志,轮功,法轮,轮法功,三去车仑,氵去车仑,发论工,法x功,法o功,法0功,法一轮一功,轮子功,车仑工力,法lun,fa轮,法lg,flg,fl功,falungong,大法弟子,大纪元,dajiyuan,明慧网,明慧周报,正见网,新唐人,伪火,退党,tuidang,退dang,超越红墙,自fen,真善忍,九评,9评,9ping,九ping,jiuping,藏字石,集体自杀,自sha,zi杀,suicide,titor,逢8必灾,逢八必灾,逢9必乱,逢九必乱,朱瑟里诺,根达亚文明,诺查丹玛斯,人类灭亡进程表,按照马雅历法,推背图,推bei图,济世灵文,诸世纪';
        $this->other = '习书记,admin,Admin,你吗B,你妈B,自杀';
    }

    /**
     * 检测字符串是否含有敏感词
     * @param $postword
     * @return bool
     */
    public function checkStr($postword)
    {
        if ($this->_checkStrName($this->people, $postword) == true ||
            $this->_checkStrName($this->salacity, $postword) == true ||
            $this->_checkStrName($this->abuse, $postword) == true ||
            $this->_checkStrName($this->superstition, $postword) == true ||
            $this->_checkStrName($this->other, $postword) == true) {
            throw new ErrorException(['code' => 406, 'msg' => '含有敏感词']);
        }
        if ($this->_msgSecCheck($postword) == true) {
            throw new ErrorException(['code' => 406, 'msg' => '含有敏感词!']);
        }
        /*if ($this->_checkStrName($this->salacity, $postword) == true) {
            return false;
        }
        if ($this->_checkStrName($this->abuse, $postword) == true) {
            return false;
        }
        if ($this->_checkStrName($this->superstition, $postword) == true) {
            return false;
        }
        if ($this->_checkStrName($this->other, $postword) == true) {
            return false;
        }*/
//        return true;
    }

    /**
     * @describe 验证敏感字符
     * @param $bannedstr $string
     * @param $string
     */
    private function _checkStrName($bannedstr, $string)
    {
        if ($bannedstr) {
            $str_arr = explode(',', $bannedstr);
            $str_banned = $this->_generateRegularExpression($str_arr);
            preg_match($str_banned, $string, $matches);
            if ($matches) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * @describe 生成正则表达式
     * @param array $words
     * @return string
     */
    private function _generateRegularExpression(array $words)
    {
        $regular = implode('|', array_map('preg_quote', $words));
        return "/$regular/i";
    }

    /**
     * 腾讯官方检测字符串是否含有敏感词接口
     * @param string $content
     * @return bool
     */
    private function _msgSecCheck($content = '')
    {
        $accessToken = (new AccessToken())->getAccessToken();
        $url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token=' . $accessToken;
        $data = ['content' => $content];
        $dataJson = json_encode($data, JSON_UNESCAPED_UNICODE);
        // $dataJson = json_encode($data,256);//此写法和上面一样
        $returnData = http_request($url, 'POST', $dataJson);
        if ($returnData['errcode'] == 0 && $returnData['errmsg'] == 'ok') {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 检测图片是否违规
     * @param $img
     * @return bool
     */
    public function imgSecCheck($img)
    {
        if ($img) {
            $obj = new \CURLFile(realpath($img));
            $obj->setMimeType("image/png");
            $accessToken = (new AccessToken())->getAccessToken();
            $url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=' . $accessToken;
            $data = ['media' => $obj];
            //$dataJson = json_encode($data, JSON_UNESCAPED_UNICODE);
            $returnData = curl_post_contents($url, $data);
            $returnDataArr = json_decode($returnData, true);
            if ($returnDataArr['errcode'] == 0 && $returnDataArr['errmsg'] == 'ok') {
                return false;
            } elseif ($returnDataArr['errcode'] == 87014) {
                return true;
            } else {
                return false;
            }

        }
    }
}